package com.java.sample.mac;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TestDemo {

    public static String getMacAddress(String ip) {
        try {
            Process p = Runtime.getRuntime().exec("arp -a " + ip);
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains(ip)) {
                    // 解析MAC地址
                    return line.split("\\s+")[1];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getMacAddress("192.168.148.1"));
    }
}
